<template>
	<view class="AdPage">
		<image style="width: 100vw;margin-bottom: 100rpx;" :src="Imgconfig.img_3" mode="widthFix">
		</image>
		<image style="width: 90vw;height: 100rpx;border-radius: 20rpx;margin-bottom: 40rpx;" :src="Imgconfig.img_2"
			mode="aspectFill" @click="sign"></image>
		<image style="width: 90vw;height: 100rpx;border-radius: 20rpx;" :src="Imgconfig.img_1" mode="aspectFill"
			@click="showAd"></image>
		<ad-rewarded-video ref="adRewardedVideo" adpid="1748657049" :preload="true" :loadnext="false" :disabled="true"
			v-slot:default="{loading, error}" @load="onadload" @close="onadclose" @error="onaderror">
			<view class="ad-error" v-if="error">{{error}}</view>
		</ad-rewarded-video>
		<Tabbar :current="2" />
		<authorize v-if="isShowAuth" @authColse="authColse" @onLoadFun="onLoadFun"></authorize>
	</view>
</template>

<script>
	import Tabbar from '@/components/tabbar/index.vue'
	import {
		getAgentAd,
		getAdimg,
		getAdForm,
		apiAdlookad
	} from '@/api/ad.js'
	import {
		getUserInfo
	} from "@/api/user";
	let rewardedVideoAd = null
	export default {
		components: {
			Tabbar
		},
		data() {
			return {
				isLoading: false,
				authAfterUrl: '',
				isShowAuth: false,
				adError: false,
				Imgconfig: {
					img_1: '',
					img_2: '',
					img_3: '',
				},
				adType: null
			}
		},

		onLoad() {
			const _this = this
			this.getData()
			this.adFrom();

		},
		onShow() {
			// 加载腾讯广告数据
		},
		methods: {
			adFrom() {
				const uid = uni.getStorageSync('UID')
				if (!uid) {
					return false
				} else {
					getAdForm().then(res => {
						console.log(res.data.ad_from === 'ad_set' ? 'adSet广告' : '腾讯广告', 'aaaaaaaaaaaaaaa');
						if (res.msg == 'ok') {
							this.adType = res.data.ad_from

							//造假数据开始
							// this.adType = 'ad_set'
							// res.data.ad_from = 'ad_set'
							//造假数据结束

							if (res.data.ad_from == 'ad_set') {
								console.log('res.data.ad_from', res.data.ad_from)
								this.isLoading = true;
								this.$refs.adRewardedVideo.load();
							} else {
								var _this = this
								if (wx.createRewardedVideoAd) {
									rewardedVideoAd = wx.createRewardedVideoAd({
										adUnitId: 'adunit-482d69fab86c4e6f'
									})
									rewardedVideoAd.onLoad(() => {
										console.log('onLoad event emit')
										console.log('激励视频 广告加载成功')
									})

									rewardedVideoAd.onError((err) => {
										console.log('onError event emit', err)
										uni.showModal({
											title: '提示',
											content: res.errCode + ":" + res.errMsg
										})
									})
									rewardedVideoAd.onClose((res) => {
										if (res.isEnded) {
											console.log('腾讯广告关闭:==完整观看', res)
											if (_this.adType == 'tencent') {
												apiAdlookad({
													adfrom: _this.adType
												}).then(res => {
													uni.showToast({
														title: res.msg
													})
												})
											}
										} else {
											console.log('腾讯广告关闭：==中途关闭', res)

										}
									})
								}
							}
						}
					})
				}



			},
			tencentAdShow() {
				rewardedVideoAd.show()
					.then(() => console.log('激励视频 广告显示'))
					.catch((err) => {
						console.log('激励视频 广告显示失败:第一次', err)
						rewardedVideoAd.load()
							.then(() => rewardedVideoAd.show())
							.catch(err2 => {
								console.log('激励视频 广告显示失败:二次', err2)
							})
					})
			},
			getData() {
				this.Imgconfig = {}
				try {
					const data = uni.getStorageSync('adImgs')
					console.log(data, 'data');
					data.forEach(item => {
						if (item.img_place == '观看广告') {
							this.Imgconfig.img_1 = item.img_url
						} else if (item.img_place == '签到图') {
							this.Imgconfig.img_2 = item.img_url
						} else if (item.img_place == '主背景图') {
							this.Imgconfig.img_3 = item.img_url
						}
					})
				} catch (e) {
					//TODO handle the exception
					getAdimg().then(res => {
						if (res.status == 200) {
							uni.setStorageSync('adImgs', res.data)
							res.data.forEach(item => {
								if (item.img_place == '观看广告') {
									this.Imgconfig.img_1 = item.img_url
								} else if (item.img_place == '签到图') {
									this.Imgconfig.img_2 = item.img_url
								} else if (item.img_place == '主背景图') {
									this.Imgconfig.img_3 = item.img_url
								}
							})
						}
					})
				}
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			// 授权回调
			onLoadFun(e) {
				this.getUserInfo(e);
				this.isShowAuth = false;
				if (this.adType) {
					return false
				}else{
					this.adFrom()
				}
			},
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					uni.setStorageSync('USER_INFO', res.data)
				});
			},
			sign() {

				const uid = uni.getStorageSync('UID')
				if (!uid) {
					this.isShowAuth = true
					return false
				}
				uni.navigateTo({
					url: '/pages/users/user_sgin/index'
				})
			},
			showAd() {
				const uid = uni.getStorageSync('UID')
				if (!uid) {
					this.isShowAuth = true
					return false
				}
				console.log('3333333', this.adType)
				if (this.adType === 'ad_set') {
					if (this.adError) {
						uni.showToast({
							title: '广告加载异常',
							icon: 'none'
						})
						return
					}

					uni.showLoading({
						title: '加载中',
						mask: true
					})
					setTimeout(() => {
						uni.hideLoading()
					}, 2000)
					this.$refs.adRewardedVideo.show();
				} else {
					this.tencentAdShow()
				}
			},
			onadload(e) {
				uni.hideLoading()
				this.isLoading = false;
				console.log('广告数据加载成功');
			},
			onadclose(e) {
				const detail = e.detail
				// 用户点击了【关闭广告】按钮
				if (detail && detail.isEnded) {
					// 正常播放结束
					if (this.adType == 'ad_set') {

						apiAdlookad({
							adfrom: this.adType
						}).then(res => {
							uni.showToast({
								title: res.msg
							})
						})
					}

				} else {
					// 播放中途退出
					console.log("onClose " + detail.isEnded);
				}
				//this.isLoading = true;
				//this.$refs.adRewardedVideo.load();
			},
			onaderror(e) {
				this.adError = true
				// 广告加载失败
				uni.hideLoading()
				console.log(e.detail);
				uni.showModal({
					title: '提示',
					content: e.detail.errMsg,
					icon: 'none',
					success(res) {
						if (res.confirm) {
							console.log('用户点击确定');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
				this.isLoading = false;
			}
		}
	}
</script>

<style scoped lang="scss">
	.AdPage {
		width: 100vw;
		height: 100vh;
		background-color: #c0ebf4;
		overflow-y: auto;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 200rpx;
	}
</style>